<?php
/**
 * This file contains all of the core achievements.
 */
function dpa_register_blogranked() { add_action( 'publish_post', 'dpa_handle_blogranked', 10, 2 ); }
function dpa_register_blogcommentranked() { add_action( 'comment_post', 'dpa_handle_blogcommentranked', 10, 2 ); }
function dpa_register_wireranked() { add_action( 'bp_wire_post_posted', 'dpa_handle_wireranked', 10, 3 ); }
function dpa_register_messageranked() { add_action( 'messages_send_message', 'dpa_handle_messageranked', 10, 1 ); }
function dpa_register_forumranked() { add_action( 'bp_forums_new_topic', 'dpa_handle_forumranked', 10, 1 ); }
function dpa_register_replyranked() { add_action( 'bp_forums_new_post', 'dpa_handle_replyranked', 10, 1 ); }
function dpa_register_blogwelcome() { add_action( 'wpmu_new_blog', 'dpa_handle_blogwelcome', 10, 2 ); }
function dpa_register_friendask() { add_action( 'friends_friendship_requested', 'dpa_handle_friendask', 10, 3 ); }
function dpa_register_grouppromoted() { add_action( 'groups_promoted_member', 'dpa_handle_grouppromoted', 10, 2 ); }
function dpa_register_groupbanned() { add_action( 'groups_banned_member', 'dpa_handle_groupbanned', 10, 2 ); }
function dpa_register_groupcreated() { add_action( 'groups_created_group', 'dpa_handle_groupcreated', 10, 1 ); }
function dpa_register_groupinvitation() { add_action( 'groups_invite_user', 'dpa_handle_groupinvitation', 10, 2 ); }
function dpa_register_groupleft() { add_action( 'groups_leave_group', 'dpa_handle_groupleft', 10, 2 ); }
function dpa_register_groupjoined() { add_action( 'groups_join_group', 'dpa_handle_groupjoined', 10, 2 ); }
function dpa_register_grouprejected() { add_action( 'groups_membership_rejected', 'dpa_handle_grouprejected', 10, 2 ); }
function dpa_register_profileupdate() { add_action( 'xprofile_updated_profile', 'dpa_handle_profileupdate', 10, 1 ); }
function dpa_register_profileactivated() { add_action( 'bp_activation_extras', 'dpa_handle_profileactivated', 10, 2 ); }
function dpa_register_profileavatar() { add_action( 'bp_core_avatar_save', 'dpa_handle_profileavatar', 10, 4 ); }
//BP #784 - function dpa_register_friendreject() { add_action( 'friends_friendship_rejected', 'dpa_handle_friendreject', 10, 1 ); }

function dpa_handle_replyranked( $post_id ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_1' ) );  //using this counter
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_100' ) );

	if ( $hundred->is_already_earnt() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->award_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->award_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->award_achievement(); }
}

function dpa_handle_forumranked( $topic_id ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_1' ) );  //using this counter
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_100' ) );

	if ( $hundred->is_already_earnt() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->award_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->award_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->award_achievement(); }	
}

function dpa_handle_messageranked( $message ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_1' ) );  //using this counter
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_100' ) );

	if ( $hundred->is_already_earnt() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->award_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->award_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->award_achievement(); }
}

function dpa_handle_wireranked( $id, $item_id, $user_id ) {
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_1' ) );  //using this counter
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_100' ) );

	if ( $hundred->is_already_earnt() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->award_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->award_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->award_achievement(); }
}

function dpa_handle_blogcommentranked( $comment_ID, $comment_approved ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_1' ) );  //using this counter
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_100' ) );

	if ( $hundred->is_already_earnt() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->award_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->award_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->award_achievement(); }
}

function dpa_handle_blogranked( $post_id, $post ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_1' ) );  //using this counter
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_100' ) );

	if ( $hundred->is_already_earnt() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->award_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->award_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->award_achievement(); }
}

function dpa_handle_profileavatar( $user_id, $old_img_path, $new_href, $img_path ) {
	$profileavatar = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'profileavatar' ) );
	if ( $profileavatar->is_already_earnt() )
	  return;

  $profileavatar->award_achievement();
}

function dpa_handle_profileactivated( $user_id, $meta ) {
	$profileactivated = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'profileactivated' ) );
	if ( $profileactivated->is_already_earnt() )
	  return;

  $profileactivated->award_achievement();
}

function dpa_handle_profileupdate( $group_id ) {
	global $bp;

	$profileupdate = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'profileupdate' ) );
	if ( $profileupdate->is_already_earnt() )
	  return;

  $profileupdate->award_achievement();
}

function dpa_handle_grouprejected( $user_id, $group_id ) {
	$grouprejected = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'grouprejected' ) );
	if ( $grouprejected->is_already_earnt() )
	  return;

  $grouprejected->award_achievement();
}

function dpa_handle_groupjoined( $group_id, $user_id ) {
	$groupjoined = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'groupjoined' ) );
	if ( $groupjoined->is_already_earnt() )
	  return;

  $groupjoined->award_achievement();
}

function dpa_handle_groupleft( $group_id, $user_id ) {
	$groupleft = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'groupleft' ) );
	if ( $groupleft->is_already_earnt() )
	  return;

  $groupleft->award_achievement();
}

function dpa_handle_groupinvitation( $group_id ) {
	global $bp;

	$groupinvitation = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'groupinvitation' ) );
	if ( $groupinvitation->is_already_earnt() )
	  return;

  $groupinvitation->award_achievement();
}

function dpa_handle_groupcreated( $group_id ) {
	global $bp;

	$groupcreated = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'groupcreated' ) );
	if ( $groupcreated->is_already_earnt() )
	  return;

  $groupcreated->award_achievement();
}

function dpa_handle_groupbanned( $user_id, $group_id ) {
	$groupbanned = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'groupbanned' ) );
	if ( $groupbanned->is_already_earnt() )
	  return;

  $groupbanned->award_achievement();
}

function dpa_handle_grouppromoted( $user_id, $group_id ) {
	$grouppromoted = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'grouppromoted' ) );
	if ( $grouppromoted->is_already_earnt() )
	  return;

  $grouppromoted->award_achievement();
}

function dpa_handle_friendask( $friendship_id, $initiator_user_id, $friend_user_id ) {
	$friendask = new dpa_achievement( array( 'user_id' => $initiator_user_id, 'name' => 'friendask' ) );
	if ( $friendask->is_already_earnt() )
	  return;

  $friendask->award_achievement();
}

function dpa_handle_blogwelcome( $blog_id, $user_id ) {
	$blogwelcome = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogwelcome' ) );
	if ( $blogwelcome->is_already_earnt() )
	  return;

  $blogwelcome->award_achievement();
}

function dpa_install_coreachievements() {
	$defaults = array(
		array( 'category' => "Blog",
					 'short_name' => 'blogwelcome',
					 'name' => "Welcome to the blogosphere",
					 'description' => "Created a blog; get posting!",
					 'points' => 15 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogranked_1',
					 'name' => "First post",
					 'description' => "Published your first blog entry.",
					 'points' => 15 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogranked_30',
					 'name' => "Flexible Demeanour",
					 'description' => "Published your thirtieth blog entry.",
					 'points' => 45 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogranked_100',
					 'name' => "The Ends Of Invention",
					 'description' => "One hundred blog posts!",
					 'points' => 75 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogcommentranked_1',
					 'name' => "It's My Party And I'll Sing If I Want To",
					 'description' => "Commented on a blog post.",
					 'points' => 10 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogcommentranked_30',
					 'name' => "But Who's Counting?",
					 'description' => "You’ve left 30 comments on blogs.",
					 'points' => 30 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogcommentranked_100',
					 'name' => "You'll Thank Me Later",
					 'description' => "You’ve left 100 comments on blogs.",
					 'points' => 50 ),
		array( 'category' => "Friends",
					 'short_name' => 'friendreject',
					 'name' => "Spurned on the altar!",
					 'description' => "[BUGGED - #784] A friendship request was rejected. Don’t worry, there are more fish in the sea!",
					 'points' => 10 ),
		array( 'category' => "Friends",
					 'short_name' => 'friendask',
					 'name' => "I Choo-Choo-Choose You... and there's a picture of a train!",
					 'description' => "You’ve sent a friendship request to a member of the site.",
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'grouppromoted',
 					 'name' => "You’ve got the Bridge, Number One",
					 'description' => "Promoted to a moderator of a group.",
					 'points' => 30 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupbanned',
					 'name' => "The peasants are revolting!",
					 'description' => "Banned someone from a group which you moderate.",
					 'points' => 30 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupcreated',
					 'name' => "Party at nine, your place.",
					 'description' => "Created a group.",
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupinvitation',
					 'name' => "All are welcome",
					 'description' => "Sent an invitation for someone to join a group which you moderate.",
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupleft',
					 'name' => "Au revoir!",
					 'description' => "You left a group; they’re bound to be missing you already.",
					 'points' => 15 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupjoined',
					 'name' => "Bonjour!",
					 'description' => "Joined a group.",
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'grouprejected',
					 'name' => "Wisdom Like Silence",
					 'description' => "A group invitation was rejected.",
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'wireranked_1',
					 'name' => "First time for everything",
					 'description' => "Posted on a wire.",
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'wireranked_30',
					 'name' => "Second time for everything",
					 'description' => "You’ve left 30 wire posts.",
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'wireranked_100',
					 'name' => "Third time for everything",
					 'description' => "You’ve left 100 wire posts.",
					 'points' => 50 ),
		array( 'category' => "Messaging",
					 'short_name' => 'messageranked_1',
					 'name' => "First Class post",
					 'description' => "Sent a message to someone.",
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'messageranked_30',
					 'name' => "Second Class post",
					 'description' => "Sent 30 messages.",
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'messageranked_100',
					 'name' => "Special Delivery",
					 'description' => "Sent 100 messages.",
					 'points' => 50 ),
		array( 'category' => "Messaging",
					 'short_name' => 'forumranked_1',
					 'name' => "Never Talk To Strangers",
					 'description' => "Started a forum topic.",
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'forumranked_30',
					 'name' => "Allow me to elaborate",
					 'description' => "Started 30 forum topics.",
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'forumranked_100',
					 'name' => "Happy Idiot Talk",
					 'description' => "Started 100 forum topics.",
					 'points' => 50 ),
		array( 'category' => "Messaging",
					 'short_name' => 'replyranked_1',
					 'name' => "Frank Exchange Of Views",
					 'description' => "Left a reply on a forum topic.",
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'replyranked_30',
					 'name' => "Revisionist",
					 'description' => "Replied to 30 forum topics.",
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'replyranked_100',
					 'name' => "Quietly Confident",
					 'description' => "Replied to 100 forum topics.",
					 'points' => 50 ),
		array( 'category' => "Profile",
					 'short_name' => 'profileupdate',
					 'name' => "Identity crisis",
					 'description' => "Updated the information on your profile.",
					 'points' => 15 ),
		array( 'category' => "Profile",
					 'short_name' => 'profileactivated',
					 'name' => "Of exceedingly good taste.",
					 'description' => "You’ve activated your account on this website - hello & welcome!",
					 'points' => 10 ),
		array( 'category' => "Profile",
					 'short_name' => 'profileavatar',
					 'name' => "She’s got that Mona Lisa smile",
					 'description' => "Uploaded or updated an avatar picture to your account.",
					 'points' => 15 )
	);

	return $defaults;
}
?>